package com.lww.integral.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lww.integral.mapper.TabCommodityMapper;
import com.lww.integral.mapper.TabGradeLogMapper;
import com.lww.integral.mapper.TabHallUsermessageMapper;
import com.lww.integral.service.ExchangeService;
import com.lww.springcloudapi.entity.TabCommodity;
import com.lww.springcloudapi.entity.TabGradeLog;
import com.lww.springcloudapi.pojo.TabHallUsermessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.Date;
import java.util.List;

@Service
public class ExchangeServiceImpl implements ExchangeService {

    @Autowired
    private TabHallUsermessageMapper tabHallUsermessageMapper;

    @Autowired
    TabCommodityMapper tabCommodityMapper;

    @Autowired
    TabGradeLogMapper tabGradeLogMapper;

    @Override
    @Transactional
    public boolean findPointsExchange(String phone, Integer cid) {

        QueryWrapper<TabHallUsermessage> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("pnumber", phone);

        //        根据用户手机号查询积分是否足够
        TabHallUsermessage tabHallUsermessage1 = tabHallUsermessageMapper.selectOne(queryWrapper);

        //        根据商品id查看库存是否足够
        TabCommodity tabCommodity1 = tabCommodityMapper.selectById(cid);
        if (tabCommodity1.getNumber() > 1) {
            if (tabCommodity1.getMoney() < tabHallUsermessage1.getIntegral()) {

                TabGradeLog tabGradeLog = new TabGradeLog();
                tabGradeLog.setUid(tabHallUsermessage1.getPnumber());
                tabGradeLog.setCid(tabCommodity1.getId());
                tabGradeLog.setNumber(1);
                tabGradeLog.setOTime(new Date());
                //添加兑换记录
                int insert = tabGradeLogMapper.insert(tabGradeLog);
                System.out.println("兑换记录添加:" + insert);

                UpdateWrapper<TabCommodity> updateWrapper = new UpdateWrapper<>();
                updateWrapper.set("Number", tabCommodity1.getNumber() - 1)
                        .eq("id", tabCommodity1.getId());
                //兑换 库存减少
                int update = tabCommodityMapper.update(null, updateWrapper);
                System.out.println("库存减少:" + update);

                UpdateWrapper<TabHallUsermessage> updateWrapper2 = new UpdateWrapper<>();
                updateWrapper2.set("integral", tabHallUsermessage1.getIntegral() - tabCommodity1.getMoney())
                        .eq("pnumber", phone);

                //用户积分减少
                int update1 = tabHallUsermessageMapper.update(null, updateWrapper2);
                System.out.println("用户积分减少:" + update1);
                if (update1 > 0 && insert > 0 && update > 0) {
                    return true;
                }
            }
        }
        //回滚
        TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        return false;
    }

    @Override
    public Page<TabGradeLog> findQuery(Integer pageNo, Integer pageSize) {
        Page<TabGradeLog> page = new Page(pageNo, pageSize);
        QueryWrapper<TabGradeLog> queryWrapper = new QueryWrapper<>();
        page = tabGradeLogMapper.selectPage(page, queryWrapper);

        return page;
    }

    @Override
    public boolean findDelGradeLog(Integer id) {
        int delete = tabGradeLogMapper.deleteById(id);
        return delete > 0;
    }
}
